#ifndef QGAME_H
#define QGAME_H

#include <QTimer>
#include <QPolygon>
#include <QList>

class QAnimal;
class QGravity;
class QColor;
class QWorldScene;
class QStatistic;
class QLife;
class Dna;
class QWorld;

/**
 *	@author Stefan Weigert <stefan.weigert@mailbox.tu-dresden.de>
 */
class QGame : QObject
{
	Q_OBJECT

	public:
		QGame ( QWorld* parent, int width, int height );

		void addQAnimalProto ( QColor color );
		void addQGravityProto ( QColor color );

		void setScene ( QWorldScene* scene );
		void setLifeStatistic ( QStatistic* stat );
		void setDnaStatistic ( QStatistic* stat );
		void setDimension ( int width, int height );
		void logLifeStatistics ( bool log );
		void logDnaStatistics ( bool log );

		int getAnimalPopulationSize();
		int getGravityPopulationSize();

		QList<QAnimal*> getQAnimals();
		QList<QGravity*> getQGravities();

		QGravity* getNextGravity ( QAnimal* animal );
		QAnimal* getNextAnimal ( QAnimal* animal );
		QLife* getIntersectingLife ( QLife* animal, QString searchForName, float viewDistance );

	public slots:
		void addQAnimal ( Dna* dna );
		void addQGravity ( Dna* dna );

		void start();
		void pause();
		void reset();
		void step();
		void changeLifeUpdateInterval ( int msec );
		void changeSceneUpdateRatio ( int ratio );
		void changeStatUpdateRatio ( int ratio );

	private:
		static const int ANIMAL=0;
		static const int GRAVITY=1;

		bool started;
		bool writeLifeLogFile;
		bool writeDnaLogFile;

		int animalPopulationSize;
		int gravityPopulationSize;
		int lifeStatTime;
		int dnaStatTime;
		int globalTime;
		int width;
		int height;
		int msec;
		int sceneRatio;
		int statRatio;

		long id;

		FILE* lifeFilePointer;
		FILE* dnaFilePointer;

		QTimer* timer;

		QWorld* world;
		QWorldScene* scene;
		QStatistic* lifeStat;
		QStatistic* dnaStat;

		QList<QAnimal*> animalList;
		QList<QGravity*> gravityList;

		// life statistics
		int gravityPopSizeID;
		int animalPopSizeID;
		int avrgAnimalNrjID;
		int avrgAnimalAgeID;
		int avrgGravityNrjID;
		// dna statistics
		int avrgMaxSpeedID;
		int avrgMaxAgeID;
		int avrgViewDistanceID;
		int avrgAttackStrengthID;
		int avrgWaitWillingnessID;

		template <typename T>
		QLife* getNextQLife ( QLife* life, float searchRadius, QList<T*> lifeList );
};

#endif
